package cn.wolfcode.rbac.web.controller;

import cn.wolfcode.rbac.domain.Department;
import cn.wolfcode.rbac.query.QueryObject;
import cn.wolfcode.rbac.service.IDepartmentService;
import cn.wolfcode.rbac.util.RequiredPermission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created by Administrator on 2020/6/10 0010.
 */
@Controller
public class DepartmentController {
    @Autowired
    private IDepartmentService departmentService;

    /**
     * 接收浏览器发送过来的请求
     * @RequestMapping("资源的路径")
     * @return 视图的路径
     */
    @RequestMapping("/department/list")
    @RequiredPermission(values = {"部门列表","department:list"})
    public String list(Model model, QueryObject queryObject){
//        <property name="prefix" value="/WEB-INF/views/"/>
//		<property name="suffix" value=".jsp"/>
        model.addAttribute("result",departmentService.query(queryObject));
        return "department/list";
    }

    @RequestMapping("/department/delete")
    @RequiredPermission(values = {"部门删除","department:delete"})
    public String delete(Long id){
        departmentService.delete(id);
        return  "redirect:/department/list.do";
    }

    @RequestMapping("/department/input")
    public String input(Model model,Long id){
        //如果id不等于null的话是编辑操作, 那么编辑操作需要把要编辑的对象共享到编辑界面当中
        if(id != null){
            model.addAttribute("entity",departmentService.get(id));
        }
        return "department/input";
    }

    /**
     * 前台传递过来了id ,name ,sn
     * 如果是新增 id是null
     * 试过是编辑 那么id就是要编辑的id的值
     * @return
     */
    @RequestMapping("/department/saveOrUpdate")
    @RequiredPermission(values = {"部门新增或删除","department:saveOrUpdate"})
    public String saveOrUpdate(Department department){
        departmentService.saveOrUpdate(department);
        System.out.println(department);
        return "redirect:/department/list.do";
    }


}
